package oj.hot100;

public class 验证二叉搜索树 {
     public class TreeNode {
         int val;
         TreeNode left;
         TreeNode right;
         TreeNode() {}
         TreeNode(int val) { this.val = val; }
         TreeNode(int val, TreeNode left, TreeNode right) {
             this.val = val;
             this.left = left;
             this.right = right;
         }
    }
    //根据二叉搜索树中序遍历一定有序的特点来判断.
    public long pre = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if(root == null) {
            return true;
        }
        //中序遍历
        boolean left = isValidBST(root.left);
        //维护前置变量,判断当前节点是否大于前置节点
        boolean cur = false;
        if(root.val > pre) {
            cur = true;
        }else {
            return false;
        }
        pre = root.val;
        boolean right = isValidBST(root.right);
        return left && cur && right;
    }
}
